define(function(require) {
	var $ = require("jquery");
	var justep = require("$UI/system/lib/justep");

	var Model = function() {
		this.callParent();
		this.groupName = null;
		this.canAddNum = justep.Bind.observable('');
		this.addShow = justep.Bind.observable(false);
	};

	Model.prototype.modelParamsReceive = function(event) {
		this.groupName = this.getContext().getRequestParameter("groupName");
		this.comp('list1').refresh();
	};

	Model.prototype.nearDataCustomRefresh = function(event) {
		// 获取附件地点列表
		var self = this;
		$.ajax({
			type : "GET",
			contentType : "application/json;charset=UTF-8",
			dataType : "json",
			async : false,
			url : require.toUrl('/wxapi/miniapp/nearby/pois?group=' + this.groupName),
			success : function(res) {
				if (res.data && res.data.poi_list) {
					self.comp('nearData').loadData(res.data.poi_list);
					self.canAddNum.set(res.data.left_apply_num);
				}
			},
			error : function(res) {
				justep.Util.hint("加载附近地点列表数据失败", {
					"type" : "warning"
				});
			}
		});
	};
	// 3：审核中，4：审核失败，5：审核通过
	Model.prototype.getAudit = function(status) {
		var str = "";
		switch (status) {
		case 3:
			str = "审核中";
			break;
		case 4:
			str = "审核失败";
			break;
		case 5:
			str = "审核通过";
			break;
		}
		return str;
	};

	Model.prototype.showBtnClick = function(event) {
		// 展示附近小程序
		var self = this;
		var row = event.bindingContext.$object;
		var options = {
			poiId : row.val('poi_id'),
			status : 1,
			group : this.groupName
		};

		$.ajax({
			type : "PUT",
			contentType : "application/json;charset=UTF-8",
			dataType : "json",
			async : false,
			url : require.toUrl('/wxapi/miniapp/nearby/showStatus?poiId='+row.val('poi_id')+'&status=1&group='+this.groupName),
			data : JSON.stringify(options),
			success : function(res) {
				if (res.success == true) {
					justep.Util.hint('展示到附近小程序成功！');
					self.comp('nearData').refreshData();
				}
			},
			error : function(res) {
				justep.Util.hint(res.responseText, {
					"type" : "warning"
				});
			}
		});
	};

	Model.prototype.cancelShowBtnClick = function(event) {
		// 取消展示附近小程序
		var self = this;
		var row = event.bindingContext.$object;
		var options = {
			poiId : row.val('poi_id'),
			status : 0,
			group : this.groupName
		};

		$.ajax({
			type : "PUT",
			contentType : "application/json;charset=UTF-8",
			dataType : "json",
			async : false,
			url : require.toUrl('/wxapi/miniapp/nearby/showStatus?poiId='+row.val('poi_id')+'&status=0&group='+this.groupName),
			success : function(res) {
				if (res.success == true) {
					justep.Util.hint('取消展示到附近小程序成功！');
					self.comp('nearData').refreshData();
				} else {
					justep.Util.hint(res.msg, {
						"type" : "warning"
					});
				}

			},
			error : function(res) {
				justep.Util.hint(res.responseText, {
					"type" : "warning"
				});
			}
		});
	};

	Model.prototype.deleteBtnClick = function(event) {
		// 取消展示附近小程序
		var self = this;
		var row = event.bindingContext.$object;

		justep.Util.confirm("确认删除当前地点？", function() {
			$.ajax({
				type : "DELETE",
				contentType : "application/json;charset=UTF-8",
				dataType : "json",
				async : false,
				url : require.toUrl('/wxapi/miniapp/nearby/poi/' + row.val('poi_id') + '?group=' + this.groupName),
				success : function(res) {
					if (res.success == true) {
						justep.Util.hint('删除地点成功！');
						self.comp('nearData').refreshData();
					} else {
						justep.Util.hint(res.msg, {
							"type" : "warning"
						});
					}
				},
				error : function(res) {
					justep.Util.hint(res.responseText, {
						"type" : "warning"
					});
				}
			});
		})

	};

	Model.prototype.addBtnClick = function(event) {
		this.addShow.set(true);
		this.comp('addData').clear();
		this.comp('addData').newData();
		$('[xid=loadtips]').text('');
	};

	Model.prototype.closeBtnClick = function(event) {
		this.addShow.set(false);
	};

	Model.prototype.cancelBtnClick = function(event) {
		this.addShow.set(false);
	};

	Model.prototype.input4Change = function(event) {
		var data = this.comp('addData');
		if (event.value&&event.value!="[]") {
			$('[xid=loadtips]').text('正在上传临时素材图片...');
			$.ajax({
				type : "POST",
				contentType : "application/json;charset=UTF-8",
				dataType : "json",
				async : false,
				url : require.toUrl('/wxapi/miniapp/material/temp'),
				data : JSON.stringify({
					"materialType" : "image",
					"storeFileName" : event.value,
					"group" : this.groupName
				}),
				success : function(res) {
					if (res && res.success == true) {
						data.val('related_proof_material', res.data.media_id);
						justep.Util.hint("临时素材上传成功");
						$('[xid=loadtips]').text('临时素材图片上传成功');
					}
				},
				error : function(res) {
					justep.Util.hint(res.responseText, {
						"type" : "warning"
					});
					$('[xid=loadtips]').text('临时素材图片上传到微信官方失败');
				}
			});
		}else{
			$('[xid=loadtips]').text('');
		}
	};

	Model.prototype.submitBtnClick = function(event) {
		var data = this.comp('addData');
		var self = this;
		var options = {
			"related_name" : data.val('related_name'),
			"related_credential" : data.val('related_credential'),
			"related_address" : data.val('related_address'),
			"group" : this.groupName
		};

		if (!data.val('related_name')) {
			justep.Util.hint("请输入经营资质主体", {
				"type" : "warning"
			});
			return;
		}
		
		if (!data.val('related_credential')) {
			justep.Util.hint("请输入经营资质证件号", {
				"type" : "warning"
			});
			return;
		}
		
		if (!data.val('related_address')) {
			justep.Util.hint("经营资质地址", {
				"type" : "warning"
			});
			return;
		}
		
		if (data.val('related_proof_material')) {
			options.related_proof_material = data.val('related_proof_material');
		}

		$.ajax({
			type : "POST",
			contentType : "application/json;charset=UTF-8",
			dataType : "json",
			async : false,
			url : require.toUrl('/wxapi/miniapp/nearby/poi'),
			data : JSON.stringify(options),
			success : function(res) {
				if (res && res.success == true) {
					justep.Util.hint("添加地点成功");
					self.addShow.set(false);
					self.comp('nearData').refreshData();
				}
			},
			error : function(res) {
				justep.Util.hint(res.responseText, {
					"type" : "warning"
				});
			}
		});
	};

	return Model;
});